package meihua.stopcarapp.widget.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import meihua.stopcarapp.R;


/**
 * Created by 小杜 on 2017/5/10.
 * 封装了EditText的Dialog样式
 */

public class CustomDialog {
    private String[] sex = {"男", "女"};

    public static final int EDIT_TEXT_NUM = 1;  //输入框
    public static final int MESSGE_NUM = 2;  //消息框
    public static final int RADIO_NUM = 3;      //单选框
    private String mMsg;
    private EditText mEditText;

    public CustomDialog(Context context, String title, String etHint, String submit, String cancle, int type) {
        create( context, title, etHint, submit, cancle, type );
    }

    public AlertDialog.Builder create(Context context, String title, final String etHint, String submit, String cancle, final int type) {
        AlertDialog.Builder builder = new AlertDialog.Builder( context );

        builder.setTitle( title != null ? title : "" );
        if (type == EDIT_TEXT_NUM) {
            View dialogView = LayoutInflater.from( context )
                    .inflate( R.layout.dialog_edittext, null );
            builder.setView( dialogView );
            mEditText = (EditText) dialogView.findViewById( R.id.et_dialog );
            mEditText.setHint( etHint != null ? etHint : "" );
        }


        if (type == MESSGE_NUM) {
            builder.setView( null );
            builder.setMessage( etHint );
        }

        builder.setPositiveButton( submit,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yesOnclickListener != null) {
                            if (type == EDIT_TEXT_NUM) {
                                mMsg = mEditText.getText().toString().trim();
                            }
                            if (type == MESSGE_NUM) {
                                mMsg = etHint;
                            }
                            yesOnclickListener.onYesClick( mMsg );
                        }
                        dialog.cancel();
                    }
                } );
        builder.setNegativeButton( cancle, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
                dialogInterface.cancel();
            }
        } );

        if (type == RADIO_NUM) {
            builder.setSingleChoiceItems( sex, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mMsg = sex[i];
                }
            } );
        }

        builder.show();
        return builder;
    }


    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        void onYesClick(String msg);
    }

    public interface onNoOnclickListener {
        void onNoClick();
    }

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) {
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) {
        this.yesOnclickListener = onYesOnclickListener;
    }


}
